<template>
	<el-card>
		<div v-if="key == this.routePath.defaultPath">
			<!-- 搜索 -->
			<el-form :inline="true" :model="searchForm">
				<el-form-item label="菜单名称">
					<el-input placeholder="请输入搜索内容" v-model="searchForm.menuName"></el-input>
				</el-form-item>
				<el-form-item label="菜单状态">
					<el-select v-model="searchForm.menuStatus" class="input-width" placeholder="全部" clearable>
						<el-option v-for="item in menuStatusOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch" >搜索</el-button>
					<el-button class="filter-item" v-if="permission_add"  type="primary" icon="el-icon-plus"  @click="handleCreate" >新增</el-button>
				</el-form-item>
			</el-form>
			<!-- 列表 -->
			<el-table :data="dataList" 
			minheight="calc(100VH - 270px)" 
			highlight-current-row border style="width: 100%" 
			:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
			row-key="id"
			
			v-loading="loading">
				<el-table-column label="名称" prop="menuName" align="center" width="150"></el-table-column>
				<el-table-column label="类型" prop="menuType" align="center" min-width width="120">
					<template slot-scope="scope">
						{{scope.row.menuType == 'dirt' ? '目录':'菜单'}}
					</template>
				</el-table-column>
				<el-table-column label="路径" prop="menuUrl" align="center" ></el-table-column>
				<el-table-column label="授权编码" prop="authCode" align="center" ></el-table-column>
				<el-table-column prop="menuIcon" label="图标" align="center" width="60">
				<template slot-scope="scope">
					<template v-if="(scope.row.menuIcon != null)">
						<svg-icon :icon-class="scope.row.menuIcon" />
					</template>
				</template>
				</el-table-column>
				<el-table-column label="排序" prop="menuSort" align="center" width="80" ></el-table-column>
				<el-table-column label="状态" prop="menuStatus" width="80" align="center">
					<template slot-scope="scope">
						<el-switch
						@change="handleStatusChange(scope.$index, scope.row)"
						:active-value="1"
						:inactive-value="2"
						v-model="scope.row.menuStatus">
						</el-switch>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center"  fixed="right">
					<template slot-scope="scope">
						<el-button v-if="(scope.row.menuType == 'menu')" type="text" size="small" @click="handleViewChildElement(scope.row.menuCode)">查看元素</el-button>
						<el-button v-if="permission_view"  type="text" size="small" @click="handleView(scope.row.id)">查看</el-button>
						<el-button v-if="permission_edit"  type="text" size="small" @click="handleEdit(scope.row.id)">修改</el-button>
						<el-button v-if="permission_delete" type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<div v-else>
			<router-view :key="key"></router-view>
		</div>
	</el-card>
</template>

<script>
import { mapGetters } from 'vuex'
import { findSystemMenuList, deleteSystemMenu ,modifyMenuStatus } from '@/api/admin/systemMenu'
export default{
	name: 'menu',
	data() {
		return {
			// 默认加载弹出层
			loading:true,
			//路由地址
			routePath:{
				defaultPath: '/base/menu',
				addPath: '/base/menu/add',
				editPath: '/base/menu/edit',
				viewPath: '/base/menu/view',
				elementPath: '/base/menu/element',
			},
			// 数据列表el-table
			dataList: [],
			// 权限按钮控制
			permission_add:false,
			permission_edit:false,
			permission_delete:false,
			permission_view:false,
			// 搜索条件
			searchForm:{
				menuName: '',
				menuStatus: '',
				parentCode: null,
				pageNum: 1,
				pageSize: 10,
			},
			parentCodeList:[],
			menuStatusOptions: [
				{
					label: '显示',
					value: 1
				},
				{
					label: '隐藏',
					value: 2
				},
			],
		}
	},
	computed:{
		key() {
			return this.$route.path
		},
		...mapGetters([
			'elementList'
		])
	},
	components:{
	},
	created(){
		if( this.$route.path === this.routePath.defaultPath){
			// 初始化获取数据列表
			this.getDateList()
			//初始化按钮权限
			this.permission_add = this.elementList['base:menu:add']
			this.permission_edit = this.elementList['base:menu:edit']
			this.permission_delete = this.elementList['base:menu:delete']
			this.permission_view = this.elementList['base:menu:detail']
		}
	},
	methods:{
		// 方法集合
		getDateList(){
			this.loading = true
			console.log(this.searchForm)
			findSystemMenuList(this.searchForm).then(res => {
				this.dataList = res.data.responseData
			})
			this.loading = false
		},
		handleView(val){
			this.$router.push({path:this.routePath.viewPath,query:{id:val}});
		},
		handleViewChildElement(val){
			this.$router.push({path:this.routePath.elementPath,query:{menuCode:val}});
		},
		handleEdit(val){
			this.$router.push({path:this.routePath.editPath,query:{id:val}});
		},
		handleDelete(val){
			this.$confirm('是否要删除?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				deleteSystemMenu(val).then(() => {
					this.$notify({
						title: '成功',
						message: '删除成功',
						type: 'success',
						duration: 2000,
						onClose: this.getDateList()
					});
				}).catch((err) => {
					this.$message.error(err.data.responseMessage);
				});
			});
		},
		handleCreate(){
			this.$router.push({path:this.routePath.addPath});
		},
		handleSearch(){
			this.getDateList();
		},
		handleStatusChange(index, row){
			this.$confirm('是否要修改该状态?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let data = {
					menuId: row.id,
					menuStatus: row.menuStatus
				}
				modifyMenuStatus(data).then(() => {
					this.$message({
						type: 'success',
						message: '修改成功!'
					});
				}).catch(e =>{
					this.$message.error(e.data.responseMessage);
					this.getDateList();
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '取消修改'
				});
				this.getDateList();
			});
		},
	},
}
</script>

